const path = require('path');
const { VueLoaderPlugin } = require('vue-loader')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
const myDate = new Date();
const dateTime = `${myDate.getFullYear()}${myDate.getMonth() + 1}${myDate.getDate()}${myDate.getHours()}${myDate.getMinutes()}`
//能源管理系统打包
module.exports = {
  mode: "production",
  entry: {
    index: './src/web/index.js',
  },
  output: {
    path: path.resolve(__dirname, './admin'),
    publicPath: "./",
    filename: `[name].${dateTime}.js`,
  },
  externals: { echarts: 'echarts' },
  resolve: {
    extensions: ['.js', '.vue'],
    alias: {
      '@': path.resolve(__dirname, './src'),
      'vue': 'vue/dist/vue.common',
      'vendor': path.resolve(__dirname, '../src/vendor'),
      'component': path.resolve(__dirname, './src/components/web'),
    }
  },
  //提取公共代码缩小入口文件体积
  optimization: {
    splitChunks: {
      name: "commons",
      chunks: "all",
      maxAsyncRequests: 2
    },
    runtimeChunk: {
      name: 'runtime'
    }
  },
  plugins: [
    new CleanWebpackPlugin(['admin']),
    new VueLoaderPlugin,
    new HtmlWebpackPlugin({
      template: './index.html',
      filename: './index.html',
      inject: true,
      favicon: path.resolve('favicon.ico'),
      chunks: ['index', 'commons', 'runtime'],
    }),
    new UglifyJSPlugin({
      uglifyOptions: {
        compress: {
          warnings: false,
          drop_debugger: true,
          drop_console: true,
        }
      },
      extractComments: true,
      cache: true,
      parallel: true,
      sourceMap: true,
    }), //开启多线程打包
  ],
  module: {
    rules: [
      { test: /\.vue$/, loader: 'vue-loader' },
      { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
      {
        test: /\.css$/, use: [{ loader: 'style-loader' }, { loader: "css-loader" },
        {
          loader: "postcss-loader",
          options: {
            plugins: [
              require('autoprefixer')("last 100 versions")
            ]
          }
        }
        ]
      },
      { test: /\.json$/, loader: 'json-loader' },
      { test: /\.(png|jpe?g|gif|svg|eot|ico|ttf|woff|woff2)(\?\S*)?$/, loader: 'file-loader?name=[name]-[hash].[ext]&publicPath=' },
    ]
  }
};